<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>会员注册</title>
  <link rel="stylesheet" href="styles/common.css">
  <style>
    /* 注册页专属样式 */
    .register-container {
      max-width: 500px;
      margin: 50px auto;
      padding: 20px;
      background-color: white;
      border-radius: 8px;
    }
  </style>
</head>
<body>
  <!-- 公共导航栏（同登录页） -->
  <header>
    <a href="index.html" class="logo">会员购物平台</a>
    <div class="nav-links">
      <a href="index.html">首页</a>
      <a href="category.html">商品分类</a>
      <a href="cart.html">购物车</a>
      <span style="color: #666; margin-right: 10px">未登录</span>
      <a href="login.html">登录</a>
      <a href="register.html" class="active">注册</a>
    </div>
  </header>

  <div class="register-container">
    <h2>会员注册</h2>
    <form id="registerForm">
      <div>
        <label>姓名：</label>
        <input type="text" id="name" placeholder="请输入真实姓名" required>
      </div>
      <div>
        <label>邮箱：</label>
        <input type="email" id="email" placeholder="请输入有效邮箱" required>
      </div>
      <div>
        <label>密码：</label>
        <input type="password" id="password" placeholder="密码至少8位" required minlength="8">
      </div>
      <div>
        <label>手机号：</label>
        <input type="tel" id="phone" placeholder="请输入11位手机号" pattern="[0-9]{11}" required>
      </div>
      <button type="submit">立即注册</button>
      <p>已有账号？<a href="login.html">立即登录</a></p>
    </form>
  </div>

  <!-- 公共页脚 -->
  <footer>
    &copy; 2025 会员购物平台. 版权所有 | <a href="#">隐私政策</a>
  </footer>

  <script src="scripts/utils.js"></script>
  <script>
    // 注册表单提交事件
    document.getElementById('registerForm').addEventListener('submit', async (e) => {
      e.preventDefault();
      const name = document.getElementById('name').value;
      const email = document.getElementById('email').value;
      const password = document.getElementById('password').value;
      const phone = document.getElementById('phone').value;
      // 调用注册API
      const response = await fetch('/api/register', {
        method: 'POST',
        body: JSON.stringify({ name, email, password, phone }),
        headers: { 'Content-Type': 'application/json' }
      });
      if (response.ok) {
        alert('注册成功，请登录');
        window.location.href = 'login.html';
      } else {
        alert('注册失败，邮箱或手机号已存在');
      }
    });
  </script>
</body>
</html>